package org.platform.processor.task.check.impl;

import com.mc.mic.core.tool.helper.SpringContextHelper;
import org.platform.constant.StationConst;
import org.platform.dao.mapper.FdKashiEventMapper;
import org.platform.dao.mapper.FdMiyunEventMapper;
import org.platform.dao.mapper.FdSanyaEventMapper;
import org.platform.dao.mapper.FdWorkSchInfoMapper;
import org.platform.mo.bussmo.task.CheckItemResult;
import org.platform.mo.db.FdKashiEvent;
import org.platform.mo.db.FdMiyunEvent;
import org.platform.mo.db.FdSanyaEvent;
import org.platform.mo.db.FdWorkSchInfo;
import org.platform.processor.task.check.ICheckItem;
import org.platform.tool.helper.ResourceHelper;
import org.platform.tool.util.TimeUtils;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.util.List;

/**
 * 该类主要是用于判断: 是否发生监测点告警
 * 检查步骤: 根据任务开始时间和结束时间,判断是否有设备报警
 *
 * @author FengJie
 * @date 2018/6/25
 */
public class MonitorWarningCheckItemImpl implements ICheckItem {
    private ResourceHelper resourceHelper;
    private FdMiyunEventMapper miyunEventMapper;
    private FdSanyaEventMapper sanyaEventMapper;
    private FdKashiEventMapper kashiEventMapper;
    private FdWorkSchInfoMapper workSchInfoMapper;

    public MonitorWarningCheckItemImpl() {
        this.resourceHelper = SpringContextHelper.getBean(ResourceHelper.class);
        this.workSchInfoMapper = SpringContextHelper.getBean(FdWorkSchInfoMapper.class);
        switch (resourceHelper.getStationName()) {
            case StationConst.KASHA_STATION:
                this.kashiEventMapper = SpringContextHelper.getBean(FdKashiEventMapper.class);
                break;
            case StationConst.MIYUN_STATION:
                this.miyunEventMapper = SpringContextHelper.getBean(FdMiyunEventMapper.class);
                break;
            case StationConst.SANYA_STATION:
                this.sanyaEventMapper = SpringContextHelper.getBean(FdSanyaEventMapper.class);
                break;
            default:

        }
    }

    @Override
    public CheckItemResult getCheckResult(String taskSerialNumber, String jobTaskID, File file) {
        CheckItemResult result = new CheckItemResult(true, "正常");

        FdWorkSchInfo workSchInfo = workSchInfoMapper.selectByPrimaryKey(taskSerialNumber);
        Long startTime = TimeUtils.strTimeToTimestampsByFormat(workSchInfo.getTrackStartTime(), null);
        Long endTime = TimeUtils.strTimeToTimestampsByFormat(workSchInfo.getTrackStopTime(), null);

        Example example = new Example(FdMiyunEvent.class);
        example.createCriteria()
                .andGreaterThanOrEqualTo("happendtime", startTime)
                .andLessThanOrEqualTo("happendtime", endTime);

        switch (resourceHelper.getStationName()) {
            case StationConst.KASHA_STATION:
                List<FdKashiEvent> kashiEventList = kashiEventMapper.selectByExample(example);
                if (!kashiEventList.isEmpty()) {
                    result.setStatus(false);
                    result.setConclusion(kashiEventList.get(0).getDeviceid() + "发生报警");
                }
                break;
            case StationConst.MIYUN_STATION:
                List<FdMiyunEvent> miyunEventList = miyunEventMapper.selectByExample(example);
                if (!miyunEventList.isEmpty()) {
                    result.setStatus(false);
                    result.setConclusion(miyunEventList.get(0).getDeviceid() + "发生报警");
                }
                break;
            case StationConst.SANYA_STATION:
                List<FdSanyaEvent> sanyaEventList = sanyaEventMapper.selectByExample(example);
                if (!sanyaEventList.isEmpty()) {
                    result.setStatus(false);
                    result.setConclusion(sanyaEventList.get(0).getDeviceid() + "发生报警");
                }
                break;
            default:
        }
        return result;
    }

}
